<template>
  <div style="width: 100%; height: 100%; display: flex">
    <div class="editBox" :class="isEdit ? 'editShadow' : 'newShadow'">
      <div>
        <span>标题：</span>
        <n-tag :bordered="false" type="success" v-if="!isEdit" style="float: right">
          新增文章
        </n-tag>
        <n-tag :bordered="false" type="warning" v-else style="float: right">编辑文章</n-tag>
      </div>
      <n-input v-model:value="article.title"></n-input>
      <div>描述：</div>
      <n-input v-model:value="article.description"></n-input>
      <div>正文：</div>
      <n-input v-model:value="article.content" type="textarea" rows="20"></n-input>
      <div style="float: right">
        <n-popconfirm @positive-click="submit">
          <template #trigger>
            <n-button type="primary" secondary>提交</n-button>
          </template>
          是否确认提交？
        </n-popconfirm>
        <n-popconfirm @positive-click="deleteItem" v-if="isEdit">
          <template #trigger>
            <n-button type="error" secondary style="margin-left: 10px">删除</n-button>
          </template>
          是否确认删除？
        </n-popconfirm>
        <n-button secondary style="margin-left: 10px" @click="cancel">取消</n-button>
      </div>
    </div>
    <ArticlePreview :article="article"></ArticlePreview>
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { getArticle, deleteArticle, createArticle, updateArticle } from '../../api/index'
import type { Article } from '../../api/index'
import ArticlePreview from '@/components/ArticlePreview.vue'
import { NInput, NButton, NTag, NPopconfirm } from 'naive-ui'

const router = useRouter()
const props = defineProps<{
  id: string
}>()

let isEdit = computed(() => {
  return props.id != '-1'
})
let article = ref({} as Article)

onMounted(async () => {
  if (isEdit.value) {
    let res = await getArticle(props.id)
    article.value = res.data.article
  }
})

const submit = async () => {
  let data = {
    title: article.value.title,
    description: article.value.description,
    content: article.value.content
  }
  if (isEdit.value) {
    await updateArticle(props.id, data)
    router.replace('/')
  } else {
    await createArticle(data)
    router.replace('/')
  }
}

const deleteItem = async () => {
  await deleteArticle(props.id)
  router.replace('/')
}

const cancel = () => {
  router.replace('/')
}
</script>

<style scoped>
.editBox {
  margin: 20px;
  margin-top: 70px;
  width: 460px;
  height: 710px;
  flex-shrink: 0;
  padding: 30px;
  border-radius: 26px;
}

.editBox > * {
  margin-bottom: 15px;
}

.newShadow {
  animation: newShadowAnim 2s linear infinite alternate;
}

@keyframes newShadowAnim {
  from {
    box-shadow: #63e2b7 0px 0px 10px 0px;
  }

  to {
    box-shadow: #63e2b7 0px 0px 30px 0px;
  }
}

.editShadow {
  animation: editShadowAnim 2s linear infinite alternate;
}

@keyframes editShadowAnim {
  from {
    box-shadow: #f2c97d 0px 0px 10px 0px;
  }

  to {
    box-shadow: #f2c97d 0px 0px 30px 0px;
  }
}
</style>
